package pers.lonestar.framework.utils.support.converters;

import java.math.BigDecimal;
import java.util.Date;

import pers.lonestar.framework.utils.support.enums.CellDataTypeEnum;
import pers.lonestar.framework.utils.support.metadata.CellData;

public class DateConverter implements Converter<Date> {
    @Override
    public Date convertToJavaObject(CellData<Date> cellData) {
        return new Date((long) (cellData.getNumberValue().doubleValue() * 24 * 3600000 - 2208988800000L));
    }

    @Override
    public CellData<Date> convertToCellData(Date date) {
        CellData<Date> cellData = new CellData<>();
        cellData.setNumberValue(BigDecimal.valueOf((date.getTime() + 2208988800000L) / 24 / 3600000));
        cellData.setType(CellDataTypeEnum.DATE);
        cellData.setData(date);
        return cellData;
    }
}
